#!/bin/bash

# init host pre-reboot
# args:
#   $1: hostname
#   $2: host IP
#   $3: nameserver

# disable selinux, firewalld and swap

sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0

systemctl disable --now firewalld.service

swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab

# disable dnsmasq, set /etc/resolv.conf
systemctl disable --now dnsmasq

echo "nameserver $3" > /etc/resolv.conf

if [[ 0 -lt $(cat /etc/NetworkManager/NetworkManager.conf|grep "dns=none"|wc -l) ]]; then
    echo "exist"
else
    sed -i -e '/\[main\]/a\dns=none' /etc/NetworkManager/NetworkManager.conf
fi

systemctl restart NetworkManager

# install base packages and update system
yum install -y epel-release
yum install -y vim wget jq psmisc socat yum-utils device-mapper-persistent-data lvm2 ipvsadm ipset sysstat conntrack libseccomp
yum update -y --exclude=kernel*

# upgrade Linux kernel
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-ml-devel kernel-ml kernel-ml-headers -y
if [[ ! 0 -eq `echo $?` ]]; then
    exit 1
fi
grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfg

# set hostname
hostnamectl set-hostname $1
echo "$2 $1" >> /etc/hosts

# set ulimit
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65535
EOF

reboot
